package com.godofgamblers.hotelmanagementsystem.handler;

import com.godofgamblers.hotelmanagementsystem.pojo.*;
import com.godofgamblers.hotelmanagementsystem.service.CartService;
import com.godofgamblers.hotelmanagementsystem.service.CommentTextService;
import com.godofgamblers.hotelmanagementsystem.service.RoomInfoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Data
@Controller
public class RoomInfoHandler {
    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private CommentTextService commentTextService;

    @Resource
    private CartService cartService;

    //房间详情页面
    @RequestMapping("/showRoom")
    @ResponseBody
    public ModelAndView findRoomByRtid(Integer room_type_id, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        //获取房间详细信息的各个方法
        RoomType roomType = roomInfoService.findRoomNameByRtid(room_type_id);
        List<RoomService> roomServices = new ArrayList<RoomService>();
        roomServices = roomInfoService.findRoomServiceByRtid(room_type_id);
        List<RoomTypePicture> roomTypePictures = new ArrayList<RoomTypePicture>();
        roomTypePictures = roomInfoService.findRoomPicByRtid(room_type_id);

        //获取评论
        List<Comment> comments = new ArrayList<Comment>();
        //从session中获取当前页
        int currentPage;
        Object object = request.getSession().getAttribute("currentPage");
        if (object == null) {
            currentPage = 1;
        } else {
            currentPage = (int) object;
        }
        System.out.println(" currentPage到底是：" + currentPage);
        CommentPage commentPage = new CommentPage();
        //将current传给CommentPage中的currentPage
        commentPage.setCurrentPage(currentPage);
        //计算分页下标
        int index = (currentPage - 1) * 4;
        comments = commentTextService.findCommentByRtid(room_type_id, index);
        //获取评论用户名
        User user = commentTextService.findUserNameByUid(room_type_id);
        //获取评论数量
        int commentNumber = commentTextService.CommentNumberByRtid(room_type_id);
        //计算总页数
        int totalPage = (commentNumber % 4 == 0) ? (commentNumber / 4) : (commentNumber / 4 + 1);
        if(totalPage==0){
            totalPage=1;
        }
        //获取评论用户等级
        User_Level user_level1 = cartService.findUserLevel(user);
        CommentPage commentPages = new CommentPage(currentPage, commentNumber, index, totalPage);

      /*  User user1 = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        //通过用户信息查到该用户的会员级别以及享有的折扣
        User_Level userLevel = cartService.findUserLevel(user1);*/

        System.out.println("commentPages=================" + commentPages);
        modelAndView.addObject("commentPage", commentPage);
        modelAndView.addObject("commentPages", commentPages);
        modelAndView.addObject("comments", comments);
        modelAndView.addObject("user", user);
        modelAndView.addObject("commentNumber", commentNumber);
        modelAndView.addObject("roomType", roomType);
        modelAndView.addObject("roomServices", roomServices);
        modelAndView.addObject("user_level1", user_level1);
        modelAndView.addObject("roomTypePictures", roomTypePictures);
        //modelAndView.addObject("userLevel",userLevel);
        modelAndView.setViewName("/front_single.html");
        return modelAndView;
    }

}
